// node scripts/dev.js reactivity -f esm
//                     要打包的包名 -f 打包格式 === argv.slice(2)

import minimist from "minimist";
import { resolve, dirname } from "path";
import { fileURLToPath } from "url";
import { createRequire } from "module";
import esbuild from "esbuild";

// node中的命令行参数通过process来获取
const args = minimist(process.argv.slice(2));
const require = createRequire(import.meta.url);
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

// console.log(args); // { _: [ 'reactivity' ], f: 'esm' }
const target = args._[0] || "reactivity"; // 打包哪个项目
const format = args.f || "iife"; // 打包后的模块化规范
// console.log(target, format); // reactivity esm

console.log("fileURLToPath", __dirname, __filename);
// /Users/soup/project/learn-project/vue-learn/vue3-lesson/scripts 文件夹路径
// /Users/soup/project/learn-project/vue-learn/vue3-lesson/scripts/dev.js 文件路径

const entry = resolve(__dirname, `../packages/${target}/src/index.ts`);
const pkg = require(`../packages/${target}/package.json`);
esbuild.context({
  entryPoints: [entry],
  outfile: resolve(__dirname, `../packages/${target}/dist/${target}.js`),
  bundle: true,
  platform: "browser", // 打包后可以给浏览器使用

  sourcemap: true,
  format,
  globalName: pkg.buildOptions?.name,


}).then((ctx) => {
  console.log('打包成功');
  return ctx.watch();
})
